package com.ruoyi.user.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import weixin.popular.client.LocalHttpClient;

import java.io.InputStream;
import java.util.logging.Logger;

/**
 * 加载微信商户号的证书
 * 此证书主要是用于退款
 *
 * @author Administrator
 * @updator 侯勇
 * @updatetime 2022-04-14
 * @update_desc 新增微信视频号直播带货二级商户号证书配置
 */
@Component
@RefreshScope
public class WeixinApiInitializer implements CommandLineRunner {

    private final Logger logger = Logger.getLogger(WeixinApiInitializer.class.getName());

    /**
     * 微信APP支付+微信H5支付商户号
     */
    @Value("${weixin.native.mchid}")
    private String wxNativeMchId;


    @Override
    public void run(String... args) {

        logger.info("原生支付+H5支付对应的商户号 -> wxNativeMchId===>" + wxNativeMchId);
//        InputStream streamNative = getClass().getClassLoader().getResourceAsStream("apiclient_cert_180426.p12");
        InputStream streamNative = getClass().getClassLoader().getResourceAsStream("apiclient_cert_1243906902_20210603.p12");
        //加载微信app支付的商户证书
        LocalHttpClient.initMchKeyStore(wxNativeMchId, streamNative);
    }
}
